#include "stdafx.h"
#include "IMultInterface2.h"
#include "MyClassFactory.h"

MyClassFactory ClassFactory;
volatile LONG LockCount;
volatile LONG OutstandingObjects;

HRESULT STDMETHODCALLTYPE MyClassFactory::QueryInterface(REFIID riid, void **ppv)
{
    if (!IsEqualIID(riid, IID_IClassFactory) && !IsEqualIID(riid, IID_IUnknown))
    {
        *ppv = 0;
        return E_NOINTERFACE;
    }
    *ppv = this;
    AddRef();
    return S_OK;
}

ULONG STDMETHODCALLTYPE MyClassFactory::AddRef()
{
    return 1;
}

ULONG STDMETHODCALLTYPE MyClassFactory::Release()
{
    return 1;
}

HRESULT STDMETHODCALLTYPE MyClassFactory::CreateInstance(IUnknown *pUnkOuter, REFIID riid, void **ppvObject)
{
    IMultInterface *pObj;
    HRESULT hr;
    if (pUnkOuter != NULL)
    {
        *ppvObject = NULL;
        return CLASS_E_NOAGGREGATION;
    }
    pObj = new IMultInterface;
    if (pObj == NULL) return E_OUTOFMEMORY;
    hr = pObj->base.QueryInterface(riid, ppvObject);
    pObj->base.Release();
    if (SUCCEEDED(hr)) InterlockedIncrement(&OutstandingObjects);
    return hr;
}

HRESULT STDMETHODCALLTYPE MyClassFactory::LockServer(BOOL fLock)
{
    if (fLock) InterlockedIncrement(&LockCount);
    else InterlockedDecrement(&LockCount);
    return S_OK;
}
